package org.summer.lawyer.handle;

import cn.hutool.core.util.ObjectUtil;
import jakarta.annotation.Resource;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.summer.common.json.utils.JsonUtils;
import org.summer.lawyer.domain.bo.spuorder.CompletedOrderBo;
import org.summer.lawyer.domain.vo.payorder.PayOrderVo;
import org.summer.lawyer.domain.vo.spuorder.SpuOrderVo;
import org.summer.lawyer.event.PaymentSuccessEvent;
import org.summer.lawyer.mail.MailService;
import org.summer.lawyer.service.IMessageService;
import org.summer.lawyer.service.ISpuOrderAssignService;
import org.summer.lawyer.service.ISpuOrderService;
import org.summer.lawyer.service.ISpuStatsService;

/**
 *
 */
@Slf4j
@Getter
@Component
public class PaymentSuccessHandle implements ApplicationListener<PaymentSuccessEvent> {

    private PayOrderVo payOrderVo;

    @Resource
    private ISpuOrderService spuOrderService;
    @Resource
    private ISpuOrderAssignService spuOrderAssignService;
    @Resource
    private ISpuStatsService spuStatsService;
    @Resource
    private SmsService smsService;
    @Resource
    private IMessageService messageService;

    @Resource
    private MailService mailService;

    @Async
    @Override
    public void onApplicationEvent( PaymentSuccessEvent event) {
        log.info("[PaymentSuccessHandle]收到支付成功事件通知, 信息：{}", JsonUtils.toJsonString(event));
        this.payOrderVo = event.getPayOrderVo();
        this.complete(payOrderVo.getOrderId());
        this.updateSpuSales(payOrderVo.getOrderId());
    }

    private void  complete(Long orderId){
        SpuOrderVo orderVo = spuOrderService.selectSpuOrderVoById(orderId);
        if("0".equals(orderVo.getDispatchable())){
            if(ObjectUtil.isNull(smsService)){
                log.info("[PaymentSuccessHandle]smsService is null");
            }
            if(ObjectUtil.isNull(mailService)){
                log.info("[PaymentSuccessHandle]mailService is null");
            }
            CompletedOrderBo bo = new CompletedOrderBo();
            bo.setOrderId(orderId);
            //completedOrderBo.setWxUserId(payOrderVo.getUserId());
            spuOrderService.completedOrder(bo);
            //
            log.info("[PaymentSuccessHandle]订单信息：{}", JsonUtils.toJsonString(orderVo));
           // messageService.userFileOrderMsg(orderVo);
          //  smsService.userFileOrderSms(orderVo.getContactPhone(),orderVo.getUserName(),orderVo.getSpuName());
            log.info("[PaymentSuccessHandle][{}]发送消息和短信成功", orderVo.getSpuName());
            //发送邮件
            mailService.sendMailBySpu(orderId);
            log.info("[PaymentSuccessHandle][{}]发送邮件成功", orderVo.getSpuName());

        }else {
            messageService.userOrderMsg(orderVo);
            smsService.userOrderSms(orderVo.getContactPhone(), orderVo.getUserName());

        }
    }

    private void updateSpuSales(Long orderId){
        SpuOrderVo orderVo = spuOrderService.selectSpuOrderVoById(orderId);
        if(orderVo != null){
            spuStatsService.updateSpuSales(orderVo.getSpuId(), orderVo.getSpuCount());
        }
    }


}
